package client.chart;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.xy.XYDataset;

import common.Data;

import client.controlpanel.Configuration;

import gui.Element;
import history.DataHistoryListener;

public class ChartElement implements Element, DataHistoryListener {
	JPanel mainFrame = new JPanel();

	private TimeSeries series;
	private static final int pastMesurementsCount = 50;
	
	private boolean cleared = true;

	public ChartElement() {
		series = new TimeSeries("Nyan temperature", "ms", null);
		series.setMaximumItemCount(pastMesurementsCount);
	}

	@Override
	public void init() {
		clearSeries();
		JButton nyanButton = new JButton("Click me to see heaven");
		nyanButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
				java.net.URI uri = null;
				try {
					uri = new java.net.URI("http://nyan.cat/");
				} catch (URISyntaxException e1) {
					e1.printStackTrace();
				}
				try {
					desktop.browse(uri);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		JButton resetChart = new JButton("Reset chart");
		resetChart.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				clearSeries();
				mainFrame.repaint();
			}
		});

		Configuration.getInstance().getDataProvider().getDataHistory()
				.registerListener(this);

		mainFrame.setLayout(new BoxLayout(mainFrame, BoxLayout.Y_AXIS));

		mainFrame.add(createChart());

		mainFrame.add(resetChart);

		// mainFrame.add(nyanButton);
	}

	private Component createChart() {

		JFreeChart chart = ChartFactory.createTimeSeriesChart(
				"Current temperature mesurement", "Time",
				"Temperature value [*C]", getDataset(), true, true, false);

		ChartPanel chartPanel = new ChartPanel(chart);

		return chartPanel;
	}

	private XYDataset getDataset() {
		TimeSeriesCollection dataset = new TimeSeriesCollection();

		dataset.addSeries(series);

		return dataset;
	}

	@Override
	public JComponent getComponent() {
		return mainFrame;
	}

	@Override
	public String getCaption() {
		return "Live chart";
	}

	@Override
	public boolean isLeftPanel() {
		return false; // Right side!
	}

	@Override
	public void notifyIncomingData() {

		if(cleared) {
			for (int i = 0; i < pastMesurementsCount; ++i) {
				series.add(new TimeSeriesDataItem(new Millisecond(new Date(
						new Date().getTime() - (pastMesurementsCount + 1 - i)*1000)), 0));
			}
			cleared = false;
		}
		
		Data data = Configuration.getInstance().getDataProvider()
				.getDataHistory().elementLast();
		series.add(new TimeSeriesDataItem(new Millisecond(data.getDate()), data
				.getTemperature()));

		series.removeAgedItems(false);

		this.mainFrame.repaint();
	}

	private void clearSeries() {
		series.clear();

		cleared = true;
	}

}
